﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public class WS05OUTPUTData : BaseFunction
    {
        public bool flagRegenerateLastMonthData;
        ArrayList ListTris = new ArrayList();

        List<WGSummary> ListWGSummary;
        string TrisInclude = "";
        DataTable tbAB50OUTPUT;
        public int ThreadCount;
        public WS05OUTPUTData()
            : base()
        {
            tbAB50OUTPUT = new DataTable("AB50OUTPUT");
            tbAB50OUTPUT.Columns.Add("SUMYEAR", typeof(string));
            tbAB50OUTPUT.Columns.Add("SUMMONTH", typeof(string));
            tbAB50OUTPUT.Columns.Add("LSLIEFNR", typeof(string));
            tbAB50OUTPUT.Columns.Add("FACTORY", typeof(string));
            tbAB50OUTPUT.Columns.Add("BATCHES", typeof(int));
            tbAB50OUTPUT.Columns.Add("SUBQTY", typeof(double));
            tbAB50OUTPUT.Columns.Add("SUBYIELDQTY", typeof(double));
            tbAB50OUTPUT.Columns.Add("G10KGORDERS", typeof(int));
            tbAB50OUTPUT.Columns.Add("OTDORDERS", typeof(int));
            tbAB50OUTPUT.Columns.Add("GOTDORDERS", typeof(int));
            tbAB50OUTPUT.Columns.Add("MAXDELAYDAYS", typeof(int));
            tbAB50OUTPUT.Columns.Add("LOTDORDERS", typeof(int));
            tbAB50OUTPUT.Columns.Add("MAXLESSDAYS", typeof(int));
        }
        public WS05OUTPUTData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }

        public DataSet GetRawData(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string VKPAUFNUM;
            string WBSRTDATE, WBENTDATE;
            WBSRTDATE = dwABPOS["WBSRTDATE"].ToString().Trim();
            WBENTDATE = dwABPOS["WBENTDATE"].ToString().Trim();

            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                #region
                or1.CommandText = "SELECT DISTINCT LSNAME1 AS FPRODUCER,WBBDPART,SUBSTR(P.ABPART,2,1) AS ABPARTTYPE"
                    +" ,TRIM(P.ABPART)||'-'||TRIM(P.ABPFARBE)||'-'||TRIM(P.ABPVAR) AS ARTICLE"
                    + ",ROUND(0.001*SUM(WBEWMTR),0) AS WBEWMTR,ROUND(0.01*SUM(P.ABPEALFM)/COUNT(*),1) AS ABPEALFM,"
                    + "ROUND(100*(ROUND(0.001*SUM(WBEWMTR),0)-ROUND(0.01*SUM(P.ABPEALFM)/COUNT(*),1))/ROUND(0.001*SUM(WBEWMTR),0),2) AS SHRINKAGE"
                    +"  FROM  ABPOS P"
                    +" LEFT JOIN LIEFDAT L ON  P.ABPLIEFNR=L.LSLIEFNR"
                    +" LEFT JOIN WBBEW W ON W.WBEWMANDANT = P.ABPMANDANT AND W.WBEWAUFKRS=P.ABPAUFKRS AND W.WBEWAUFLFD=P.ABPAUFLFD AND W.WBEWAUFPOS=P.ABPAUFPOS"
                    +" AND W.WBEWWAKZ='R' AND W.WBEWBS='21' AND W.WBEWAFOLGE='010' AND W.WBEWFC='AV10'"
                    +" LEFT JOIN WBBST B ON  W.WBEWMANDANT = B.WBBMANDANT AND W.WBEWRWKRS = B.WBBRWKRS AND W.WBEWRWLFD = B.WBBRWLFD AND W.WBEWPOS = B.WBBPOS"
                    +" WHERE P.ABPAUFART='VA' AND ABPERLKZ='J'"
                    + " GROUP BY LSNAME1,SUBSTR(P.ABPART, 2, 1),TRIM(P.ABPART)||'-'||TRIM(P.ABPFARBE)||'-'||TRIM(P.ABPVAR),WBBDPART"
                    +",ABPAUFKRS,ABPAUFLFD,ABPAUFPOS"
                    +" ORDER BY 1,2,3,4"; 
                   
                #endregion
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "WS05OUTPUT";
                    SetDefault(tableLocal);
                    tbAB50OUTPUT.Clear();
                    byte[] b;
                    string value;
                    string sTemp = "";
                    string Column = "";
                    string ARTICLE = "", WBBDPART, FACTORY;
                    int PCNUM;
                    double WBEWMTR;
                    DataRow dwOUTPUT;
                    List<DataRow> dltRows = new List<DataRow>();
                    ListWGSummary = new List<WGSummary>();
                    string SUMMONTH, SUMYEAR, LSLIEFNR;
                    SUMYEAR = WBSRTDATE.Substring(0, 4);
                    SUMMONTH = WBSRTDATE.Substring(4, 2);
                    int G10KGORDERS;
                    int dYear, dMonth, dDay, eYear, eMonth, eDay;
                    string ABPLIEFDAT, ABPERLDAT;
                    DateTime dDate, eDate;
                    bool hasdDate = false, haseDate = false;
                    bool flagnew = false;
                    string VKPARTTYPE, VKPPOSTYP;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        Column = "FPRODUCER";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        Column = "ARTICLE";
                        #endregion
                        Column = "ARTICLE";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "WBBDPART";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        VKPARTTYPE = dwTemp["ABPARTTYPE"].ToString();
                        switch (VKPARTTYPE)
                        {
                            case "1":
                                dwTemp["ABPARTTYPE"] = "市场";
                                break;
                            case "3":
                                dwTemp["ABPARTTYPE"] = "高档";
                                break;
                            case "2":
                                dwTemp["ABPARTTYPE"] = "色织";
                                break;
                            case "4":
                                dwTemp["ABPARTTYPE"] = "色织";
                                break;
                            default:
                                dwTemp["ABPARTTYPE"] = "未知";
                                break;
                        } 
                    }
                     
                    setLocal.AcceptChanges();
                    set4Return.Merge(setLocal); 
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }

        public DataSet BuildWBBST(DataRow dwUI)
        {
            string progress = "查询完成";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
                setResult = GetRawData(dwUI);
                setReturn.Merge(setResult);
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}